package com.photo.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.photo.system.annotation.AuthCheck;
import com.photo.system.common.BaseResponse;
import com.photo.system.common.DeleteRequest;
import com.photo.system.common.ErrorCode;
import com.photo.system.common.ResultUtils;
import com.photo.system.constant.UserConstant;
import com.photo.system.exception.BusinessException;
import com.photo.system.exception.ThrowUtils;
import com.photo.system.model.dto.categories.CategoriesAddRequest;
import com.photo.system.model.dto.categories.CategoriesUpdateRequest;
import com.photo.system.model.entity.Categories;
import com.photo.system.model.vo.CategoriesVO;
import com.photo.system.service.CategoriesService;
import com.photo.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 摄影产品分类接口
 *
 * @author xuwenhui
 */
@RestController
@RequestMapping("/categories")
@Slf4j
@Api(tags = {"摄影产品分类接口"})
public class CategoriesController {

    @Resource
    private CategoriesService categoriesService;

    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 创建分类
     *
     * @param categoriesAddRequest
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "创建摄影产品分类")
    public BaseResponse<Long> addCategories(@RequestBody CategoriesAddRequest categoriesAddRequest) {
        if (categoriesAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Categories categories = new Categories();
        BeanUtils.copyProperties(categoriesAddRequest, categories);
        boolean result = categoriesService.save(categories);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "创建失败");
        return ResultUtils.success(categories.getId());
    }

    /**
     * 删除摄影产品分类（仅管理员）
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "删除摄影产品分类（仅管理员）")
    public BaseResponse<Boolean> deleteCategories(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        boolean result = categoriesService.removeById(deleteRequest.getId());
        return ResultUtils.success(result);
    }

    /**
     * 更新摄影产品分类
     *
     * @param categoriesUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation(value = "更新摄影产品分类")
    public BaseResponse<Boolean> updateCategories(@RequestBody CategoriesUpdateRequest categoriesUpdateRequest) {
        if (categoriesUpdateRequest == null || categoriesUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        Categories categories = new Categories();
        BeanUtils.copyProperties(categoriesUpdateRequest, categories);
        boolean result = categoriesService.updateById(categories);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新失败");
        return ResultUtils.success(result);
    }


    /**
     * 获取所有分类
     *
     * @param request
     * @return
     */
    @PostMapping("/list/all")
    @ApiOperation(value = "获取所有摄影产品分类")
    public BaseResponse<List<CategoriesVO>> listAllCategories(HttpServletRequest request) {
        QueryWrapper<Categories> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        List<Categories> categoriesList = categoriesService.list(queryWrapper);
        List<CategoriesVO> categoriesVOList = categoriesList.stream().map(categories -> {
            CategoriesVO categoriesVO = new CategoriesVO();
            BeanUtils.copyProperties(categories, categoriesVO);
            return categoriesVO;
        }).toList();
        return ResultUtils.success(categoriesVOList);
    }

}
